package com.luoxiang.socialandroid.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.THost;

import java.util.List;

public class OnlineAdapter extends RecyclerView.Adapter<OnlineAdapter.ViewHolder> {
    private List<THost> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    public void setData(List<THost> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int userid); //传递boolean类型数据给activity
    }

    OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    @NonNull
    @Override
    public OnlineAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ol, parent, false);
        OnlineAdapter.ViewHolder holder = new OnlineAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull OnlineAdapter.ViewHolder holder, int position) {
        THost hostBean = list.get(position);
        RoundedCorners roundedCorners = new RoundedCorners(30);
        RequestOptions options1 = RequestOptions.bitmapTransform(roundedCorners);
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_error_img)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_error_img) //url为空的时候,显示的图片
                .error(R.drawable.icon_error_img);//图片加载失败后，显示的图片
        Glide.with(MyAppliaction.getContext())
                .load(url + hostBean.getIcon())
                .transform(new CenterCrop(), new RoundedCorners(30))
                .into(holder.im1_ol);
        if (hostBean.getHost()!=1){
            holder.img_toonline.setVisibility(View.GONE);
            if (hostBean.getIdentity() == 10) {
                holder.im2_ol.setImageResource(R.mipmap.icon_huangjin);
                holder.im2_ol.setVisibility(View.VISIBLE);
            } else if (hostBean.getIdentity() == 20) {
                holder.im2_ol.setImageResource(R.mipmap.icon_bojin);
                holder.im2_ol.setVisibility(View.VISIBLE);
            } else if (hostBean.getIdentity() == 30) {
                holder.im2_ol.setImageResource(R.mipmap.icon_zuanshi);
                holder.im2_ol.setVisibility(View.VISIBLE);
            } else {
                holder.im2_ol.setVisibility(View.GONE);
            }
        }else {
            holder.img_toonline.setImageResource(R.mipmap.icon_online);
            holder.img_toonline.setVisibility(View.VISIBLE);
            holder.im2_ol.setVisibility(View.GONE);
        }


        holder.icon_txname_bg.bringToFront();
        holder.tx_ol.setText(hostBean.getName());
        holder.tx_ol.bringToFront();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClick.onItemClicks(hostBean.getUserId());
            }
        });
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im1_ol, im2_ol, icon_txname_bg, img_toonline;
        private TextView tx_ol;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            icon_txname_bg = itemView.findViewById(R.id.icon_txname_bg);
            im1_ol = itemView.findViewById(R.id.im1_ol);
            im2_ol = itemView.findViewById(R.id.im2_ol);
            tx_ol = itemView.findViewById(R.id.tx_ol);
            img_toonline = itemView.findViewById(R.id.img_toonline);
        }
    }
}
